package com.yuanchu.marketing.listener;

import com.alibaba.fastjson.JSON;
import com.yuanchu.common.exception.YuanChuException;
import com.yuanchu.marketing.dto.OrderMessage;
import com.yuanchu.marketing.service.YoshopUserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.yuanchu.marketing.config.DearMessageConfig.PAYNOTIFY_MARKETING_QUEUE;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-23 17:08
 */
@Slf4j
@Component
public class OrderListener {

    @Resource
    private YoshopUserCouponService userCouponService;

    // @RabbitListener(bindings = @QueueBinding(
    //         value = @Queue(name = PAYNOTIFY_MARKETING_QUEUE),
    //         exchange = @Exchange(name = "paynotify_exchange_direct"),
    //         key = {"market"}
    // ))

    //监听消息队列接收支付结果通知
    @RabbitListener(queues = PAYNOTIFY_MARKETING_QUEUE)
    public void listenSimpleQueue(String msg) {
        // 接收到消息
        log.info("消费者接收到simple.queue的消息：【{}】", msg);
        OrderMessage message = JSON.parseObject(msg, OrderMessage.class);
        Integer couponId = message.getCouponId();
        if (couponId == null || couponId == 0) {
            return;
        }
        // 处理消息, 修改用户优惠券为已使用
        boolean flage = userCouponService.updateUserCouponStatus(message.getUserId(), couponId, message.getStoreId());
        if (!flage) {
            throw new YuanChuException("更新优惠券状态失败");
        }
        log.info("消息处理完成！");
    }


    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg){
        System.out.println("消费者接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg){
        System.out.println("消费者接收到direct.queue2的消息：【" + msg + "】");
    }

}
